package terra.model;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;

import terra.Config;
import terra.definitions.item.ItemDef;
import terra.managers.DefManager;
import terra.managers.LightManager;
import terra.managers.ResourceManager;
import terra.network.packet.server.ObjectMove;
import terra.slick.GameState;
import terra.util.FontUtils;
import terra.util.MathUtil;

public class Player extends GameObject {

	private String name = "";
	private int con,str,dex;
	private int currentHp,maxHp;
	private int currentBreath,maxBreath,atk,def,critChance,critPower;
	private long exp,expToLvl;
	Animation[] animations = new Animation[ObjectMove.ANIMATION_LADDER+1];
	
	Image head = ResourceManager.getImage("player_head");
	Animation run =  ResourceManager.getAnimation("player_4").copy();
	
	public Player(){
		setType(ObjectType.PLAYER);
		animations[ObjectMove.ANIMATION_ATTACK] = ResourceManager.getAnimation("player_"+ObjectMove.ANIMATION_ATTACK).copy();
		animations[ObjectMove.ANIMATION_MOVE] = ResourceManager.getAnimation("player_"+ObjectMove.ANIMATION_MOVE).copy();
		animations[ObjectMove.ANIMATION_STAND] = ResourceManager.getAnimation("player_"+ObjectMove.ANIMATION_STAND).copy();
		animations[ObjectMove.ANIMATION_FALL] = ResourceManager.getAnimation("player_"+ObjectMove.ANIMATION_STAND).copy();
		animations[ObjectMove.ANIMATION_JUMP] = ResourceManager.getAnimation("player_"+ObjectMove.ANIMATION_STAND).copy();
		animations[ObjectMove.ANIMATION_LADDER] = ResourceManager.getAnimation("player_"+ObjectMove.ANIMATION_LADDER).copy();
	}
	
	public void update(int delta){
//		if (getAnimationCurrent() != getAnimationOld()){
//			animations[getAnimationOld()].restart();
//			animations[getAnimationCurrent()].restart();
//			setAnimationOld(getAnimationCurrent());
//		}
		run.update(delta);
	}
	
	public void restartAnimation(int current,int old){
		
	}
	
	public void render(Graphics g){
		if (getAnimationCurrent() == ObjectMove.ANIMATION_STAND){
			animations[getAnimationCurrent()].draw(getX(), getY());
		} else if (getAnimationCurrent() == ObjectMove.ANIMATION_MOVE){
			if (isLeft())	animations[getAnimationCurrent()].getCurrentFrame().getFlippedCopy(true, false).draw(getX()-2, getY());
			 else			animations[getAnimationCurrent()].getCurrentFrame().draw(getX()+2, getY());
			animations[getAnimationCurrent()].draw();
		} else {
			animations[getAnimationCurrent()].draw(getX(), getY());
		}
		if (getHeading() >= 0 && getHeading() <= 180){
			head.setRotation(0);
			g.rotate(getX()+getW()/2, getY()+4,MathUtil.constrain(getHeading(), 40, 140)-90 );
			head.draw(getX(), getY()-6);	
			g.rotate(getX()+getW()/2, getY()+4,-(MathUtil.constrain(getHeading(), 40, 140)-90));
		} else {
			Image h = head.getFlippedCopy(true, false);
			g.rotate(getX()+getW()/2, getY()+4,MathUtil.constrain(getHeading(), -140, -40)+90 );
			h.draw(getX(), getY()-6);	
			g.rotate(getX()+getW()/2, getY()+4,-(MathUtil.constrain(getHeading(), -140, -40)+90));
			
		}
		FontUtils.drawCenter(GameState.font10, this.getName()+" ",(int) getX(),(int) getY()-20, 16);
		

	}	
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getCon() {
		return con;
	}

	public void setCon(int con) {
		this.con = con;
	}

	public int getStr() {
		return str;
	}

	public void setStr(int str) {
		this.str = str;
	}

	public int getDex() {
		return dex;
	}

	public void setDex(int dex) {
		this.dex = dex;
	}

	public int getCurrentHp() {
		return currentHp;
	}

	public void setCurrentHp(int currentHp) {
		this.currentHp = currentHp;
	}

	public int getMaxHp() {
		return maxHp;
	}

	public void setMaxHp(int maxHp) {
		this.maxHp = maxHp;
	}

	public int getCurrentBreath() {
		return currentBreath;
	}

	public void setCurrentBreath(int currentBreath) {
		this.currentBreath = currentBreath;
	}

	public int getMaxBreath() {
		return maxBreath;
	}

	public void setMaxBreath(int maxBreath) {
		this.maxBreath = maxBreath;
	}

	public long getExp() {
		return exp;
	}

	public void setExp(long exp) {
		this.exp = exp;
	}
	


	public long getExpToLvl() {
		return expToLvl;
	}

	public void setExpToLvl(long expToLvl) {
		this.expToLvl = expToLvl;
	}

	public int getAtk() {
		return atk;
	}

	public void setAtk(int atk) {
		this.atk = atk;
	}

	public int getDef() {
		return def;
	}

	public void setDef(int def) {
		this.def = def;
	}

	public int getCritChance() {
		return critChance;
	}

	public void setCritChance(int critChance) {
		this.critChance = critChance;
	}

	public int getCritPower() {
		return critPower;
	}

	public void setCritPower(int critPower) {
		this.critPower = critPower;
	}	
	
	public void setAnimationCurrent(byte animationCurrent) {
		super.setAnimationCurrent(animationCurrent);
		if (getAnimationCurrent() != getAnimationOld()){
			animations[getAnimationOld()].restart();
			animations[getAnimationCurrent()].restart();
			setAnimationOld(getAnimationCurrent());
		}
	}
	
}
